var heatCircle;
var colorBar = [
  "rgba(255, 255, 255, 0)",
  "rgba(22, 211, 179, 0.2)",
  "rgba(78, 240, 141, 0.2)",
  "rgba(237, 232, 112, 0.2)",
  "rgba(250, 163, 110, 0.2)",
  "rgba(239, 102, 86, 0.2)",
  "rgba(220, 20, 60, 0.2)",
];
function drawHeatCircle(_voiceVal) {
  heatCircleLayer.eachGraphic((graphic) => {
    heatCircleLayer.removeGraphic(graphic);
  });
  switch (true) {
    case _voiceVal < 60:
      drawCircle(getHeatRadius(_voiceVal), getHeatColor(_voiceVal));
      break;
    case 60 <= _voiceVal && _voiceVal < 70:
      drawCircle(10, getHeatColor(_voiceVal));
      drawCircle(getHeatRadius(_voiceVal), colorBar[1]);
      break;
    case 70 <= _voiceVal && _voiceVal < 75:
      drawCircle(10, getHeatColor(_voiceVal));
      drawCircle(31, colorBar[2]);
      drawCircle(getHeatRadius(_voiceVal), colorBar[1]);
      break;
    case _voiceVal >= 75:
      drawCircle(10, getHeatColor(_voiceVal));
      drawCircle(31, colorBar[3]);
      drawCircle(56, colorBar[2]);
      drawCircle(getHeatRadius(_voiceVal), colorBar[1]);
      break;
  }
}

function drawCircle(_radius, _color) {
  var circle = new mars3d.graphic.CirclePrimitive({
    position: [113.501912, 22.220651, 10.9],
    style: {
      radius: _radius,
      materialType: mars3d.MaterialType.PolyGradient,
      materialOptions: {
        color: _color,
        alphaPower: 1.5,
        diffusePower: 1,
      },
      clampToGround: true, // 是否贴地
    },
  });
  heatCircleLayer.addGraphic(circle);
}

function getHeatColor(_voiceVal) {
  var MaterialColor;
  switch (true) {
    case 0 < _voiceVal && _voiceVal <= 41:
      MaterialColor = colorBar[0];
      break;
    case 41 < _voiceVal && _voiceVal <= 50:
      MaterialColor = colorBar[1];
      break;
    case 50 < _voiceVal && _voiceVal <= 60:
      MaterialColor = colorBar[2];
      break;
    case 60 < _voiceVal && _voiceVal <= 70:
      MaterialColor = colorBar[3];
      break;
    case 70 < _voiceVal && _voiceVal <= 75:
      MaterialColor = colorBar[4];
      break;
    case 75 < _voiceVal && _voiceVal <= 80:
      MaterialColor = colorBar[5];
      break;
    case 80 < _voiceVal:
      MaterialColor = colorBar[6];
      break;
    default:
      MaterialColor = colorBar[0];
      break;
  }
  return MaterialColor;
}

function initHeatCircle(_voiceVal) {
  var val = parseFloat(_voiceVal);
  switch (true) {
    case val < 60:
      drawHeatZone([113.508267, 22.223133], 1, "#ffff00", 1, 1, 80);
      break;
    case 60 <= val && val < 80:
      drawHeatZone([113.508267, 22.223133], 1, "#ff0000", 1, 1, 80);
      drawHeatZone([113.508267, 22.223133], 10, "#ffff00", 0.8, 2, 60);
      break;
    case 80 <= val && val < 100:
      drawHeatZone([113.508267, 22.223133], 1, "#ff0000", 1, 1, 80);
      drawHeatZone([113.508267, 22.223133], 10, "#ffff00", 0.8, 2, 60);
      drawHeatZone([113.508267, 22.223133], 100, "#00ff00", 0.7, 5, 40);
      break;
    case val >= 100:
      drawHeatZone([113.508267, 22.223133], 1, "#ff0000", 1, 1, 80);
      drawHeatZone([113.508267, 22.223133], 10, "#ffff00", 0.8, 2, 60);
      drawHeatZone([113.508267, 22.223133], 100, "#00ff00", 0.7, 5, 40);
      drawHeatZone([113.508267, 22.223133], 1000, "#fff", 0.5, 5, 40);
      break;
  }
}
// 传入源的分贝值，获取40分贝时的距离
function getHeatRadius(_voiceVal) {
  var radius = parseFloat((10 ** ((_voiceVal - 40) / 20)).toFixed(2));
  return radius;
}
// 传入距离和源的分贝值，获取该距离时的分贝
function getHeatVal(_voiceVal, _radius) {
  var voiceVal = _voiceVal - 20 * Math.log10(_radius);
  return voiceVal;
}

var vDataList = [70.4, 80.2, 90, 65, 49, 88, 85, 73, 77, 74, 52];
var voIndex = 0;
function drawHeatList() {
  var drawHeatListInt = setInterval(() => {
    if (voIndex < vDataList.length) {
      drawCanvasPos(vDataList[voIndex]);
      voIndex++;
    } else {
      // voIndex = 0;
      clearInterval(drawHeatListInt);
    }
  }, 1000);
}

var circle = null;
function drawCanvasPos(_pos, _voiceVal) {
  createHeatDom();
  drawHeatCanvas(_voiceVal);
  // if (circle != null) {
  //   heatCircleLayer.removeGraphic(circle);
  // }
  circle = new mars3d.graphic.CirclePrimitive({
    position: [_pos[0], _pos[1], 10],
    style: {
      radius: getHeatRadius(_voiceVal) < 500 ? getHeatRadius(_voiceVal) : 500,
      materialType: mars3d.MaterialType.Image,
      materialOptions: {
        // image: new Cesium.CallbackProperty(heatCanvas, true),
        image: heatcvs,
        transparent: true,
        // repeat: new Cesium.Cartesian2(2.0, 2.0),
      },
      clampToGround: true, // 是否贴地
    },
  });
  heatCircleLayer.addGraphic(circle);
}

var heatcvs;
var ctx;
// 创建热力图canvas实例
function createHeatDom() {
  heatcvs = mars3d.DomUtil.create("canvas", "heatcanvas", document.body);
  heatcvs.width = 200;
  heatcvs.height = 200;
}
// 热力图canvas绘制方法
function drawHeatCanvas(_voiceVal) {
  ctx = heatcvs.getContext("2d");

  //绘制一个以坐标点(100,100)为圆心、半径为150px的圆形
  ctx.arc(100, 100, 100, 0, Math.PI * 2, false);
  //该对象的作用域是以(100,100)为圆心、半径为10px的内圆和以(100,100)为圆心、半径为50px的外圆之间的环状区域
  var canvasGradient = ctx.createRadialGradient(100, 100, 10, 100, 100, 60);
  // canvasGradient.addColorStop(0, colorBar[5]);
  // canvasGradient.addColorStop(0.3, colorBar[4]);
  // canvasGradient.addColorStop(0.5, colorBar[3]);
  // canvasGradient.addColorStop(0.8, colorBar[2]);
  // canvasGradient.addColorStop(1, colorBar[1]);
  // canvasGradient.addColorStop(0, getHeatColor(_voiceVal));
  // canvasGradient.addColorStop(0.3, getHeatColor(_voiceVal));
  // canvasGradient.addColorStop(0.5, getHeatColor(_voiceVal));
  // canvasGradient.addColorStop(0.8, getHeatColor(_voiceVal));
  // canvasGradient.addColorStop(1, getHeatColor(_voiceVal));

  switch (true) {
    case _voiceVal < 41:
      canvasGradient.addColorStop(1, colorBar[1]);
      break;
    case 41 <= _voiceVal && _voiceVal < 50:
      canvasGradient.addColorStop(0.3, colorBar[2]);
      canvasGradient.addColorStop(1, colorBar[1]);
      break;
    case 50 <= _voiceVal && _voiceVal < 60:
      canvasGradient.addColorStop(0.3, colorBar[3]);
      canvasGradient.addColorStop(0.5, colorBar[2]);
      canvasGradient.addColorStop(1, colorBar[1]);
      break;
    case 60 <= _voiceVal && _voiceVal < 70:
      canvasGradient.addColorStop(0.3, colorBar[4]);
      canvasGradient.addColorStop(0.5, colorBar[3]);
      canvasGradient.addColorStop(0.7, colorBar[2]);
      canvasGradient.addColorStop(1, colorBar[1]);
      break;
    case 70 <= _voiceVal && _voiceVal < 75:
      canvasGradient.addColorStop(0.1, colorBar[5]);
      canvasGradient.addColorStop(0.3, colorBar[4]);
      canvasGradient.addColorStop(0.5, colorBar[3]);
      canvasGradient.addColorStop(0.7, colorBar[2]);
      canvasGradient.addColorStop(1, colorBar[1]);
      break;
    case 75 <= _voiceVal && _voiceVal < 80:
      canvasGradient.addColorStop(0.1, colorBar[6]);
      canvasGradient.addColorStop(0.3, colorBar[5]);
      canvasGradient.addColorStop(0.3, colorBar[4]);
      canvasGradient.addColorStop(0.5, colorBar[3]);
      canvasGradient.addColorStop(0.7, colorBar[2]);
      canvasGradient.addColorStop(1, colorBar[1]);
      break;
    case _voiceVal >= 80:
      canvasGradient.addColorStop(0.2, colorBar[6]);
      canvasGradient.addColorStop(0.3, colorBar[5]);
      canvasGradient.addColorStop(0.4, colorBar[4]);
      canvasGradient.addColorStop(0.5, colorBar[3]);
      canvasGradient.addColorStop(0.7, colorBar[2]);
      canvasGradient.addColorStop(1, colorBar[1]);
      break;
  }

  //将fillStyle的属性值设为该CanvasGradient对象
  ctx.fillStyle = canvasGradient;
  ctx.fill();
}

function convertCanvasToImage(canvas) {
  var image = new Image();
  image.src = canvas.toDataURL("image/png");
  return image;
}
